//
//  ImageAddPreView.m
//  TestAPP
//
//  Created by yangyong on 14-6-3.
//  Copyright (c) 2014年 gainline. All rights reserved.
//

#import "JSImageAddPreView.h"
#import "UIColor+Help.h"
#import "UIButton+Help.h"

#define D_Pending_Width         6

#define D_ImageEditView_Width   84
#define D_ImageEditView_Height  84
@implementation JSImageAddPreView

+ (void)show
{
    
}

+ (void)hide
{
    
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initResource];
    }
    return self;
}



- (void)initResource
{
    _imageassets = [NSMutableArray array];
    _imageEditViewArray = [NSMutableArray array];
    //TODO
    self.backgroundColor = [UIColor colorWithHex:0x171819 alpha:0.9];

    _selectBeforeLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 30)];
    [_selectBeforeLab setTextColor:[UIColor whiteColor]];
    [_selectBeforeLab setFont:[UIFont systemFontOfSize:14.0f]];
    [self addSubview: _selectBeforeLab];
    [_selectBeforeLab setTextAlignment:NSTextAlignmentCenter];
    //TODO
    [_selectBeforeLab setText:NSLocalizedString(@"可以选择1-4张照片", @"")];
    
    _startPintuButton =  [[JSCircleButton alloc] initWithFrame:CGRectMake(self.bounds.size.width-84, 48, D_ImageEditView_Width - 10, D_ImageEditView_Height -10)];
    _startPintuButton.btnColor = [UIColor whiteColor];
    _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 32)];
    _countLabel.font = [UIFont systemFontOfSize:30];
    _countLabel.text = @"0/4";
    [_startPintuButton addSubview:_countLabel];
    [_startPintuButton setTitle:NSLocalizedString(@"开始制作", @"") forState:UIControlStateNormal];
    [_startPintuButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_startPintuButton setTitleEdgeInsets:UIEdgeInsetsMake(30, 0, 0, 0)];
    _startPintuButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [_startPintuButton addTarget:self action:@selector(startPintuButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_startPintuButton.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
    [_startPintuButton.layer setCornerRadius:2.f];
    [_startPintuButton setClipsToBounds:YES];
    [self addSubview:_startPintuButton];
    _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, self.bounds.size.width - 90, D_ImageEditView_Height + 8)];
    _contentView.backgroundColor = [UIColor clearColor];
    [self addSubview:_contentView];
    _contentView.showsHorizontalScrollIndicator = NO;
    _speratorView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, self.frame.size.width, 0.5)];
    _speratorView.backgroundColor = [UIColor colorWithHex:0xffffff alpha:0.1];
    [self addSubview:_speratorView];
}


- (void)addImageWith:(ALAsset *)asset
{
    if(_imageassets.count < 4){
        [_imageassets addObject:asset];
        _contentView.contentSize = CGSizeMake([_imageassets count] * D_ImageEditView_Width, _contentView.frame.size.height);
        if ([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width > _contentView.frame.size.width) {
            [self.contentView setContentOffset:CGPointMake(self.contentView.contentSize.width - _contentView.frame.size.width, 0) animated:YES];
        }
        JSImageEditView *editView = [[JSImageEditView alloc] initWithFrame:CGRectMake([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width,
                                                                                      8,
                                                                                      D_ImageEditView_Width,
                                                                                      D_ImageEditView_Height)];
        [editView setImageAsset:asset index:[_imageassets count]-1];
        editView.deleteEdit = self;
        [_imageEditViewArray addObject:editView];
        [_contentView addSubview:editView];
        [self resetContentView];
        if ([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width > _contentView.frame.size.width) {
            [self.contentView setContentOffset:CGPointMake(self.contentView.contentSize.width - _contentView.frame.size.width, 0) animated:YES];
        }
        
    }else{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:D_LocalizedCardString(@"card_meitu_max_image_prompt")
//                                                    message:D_LocalizedCardString(@"card_meitu_max_image_prompttext")
//                                                   delegate:self
//                                          cancelButtonTitle:nil
//                                          otherButtonTitles:D_LocalizedCardString(@"card_meitu_max_image_promptDetermine"), nil];
//        [alert show];
    }

}

- (void)addImageWithImageItem:(PGJSImageItem*)imageItem
{
    if(_imageassets.count < 4){
        [_imageassets addObject:imageItem];
        _contentView.contentSize = CGSizeMake([_imageassets count] * D_ImageEditView_Width, _contentView.frame.size.height);
        if ([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width > _contentView.frame.size.width) {
            [self.contentView setContentOffset:CGPointMake(self.contentView.contentSize.width - _contentView.frame.size.width, 0) animated:YES];
        }
        JSImageEditView *editView = [[JSImageEditView alloc] initWithFrame:CGRectMake([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width,
                                                                                      8,
                                                                                      D_ImageEditView_Width,
                                                                                      D_ImageEditView_Height)];
        [editView setImageItem:imageItem index:[_imageassets count] - 1];
        editView.deleteEdit = self;
        [_imageEditViewArray addObject:editView];
        [_contentView addSubview:editView];
        [self resetContentView];
        if ([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width > _contentView.frame.size.width) {
            [self.contentView setContentOffset:CGPointMake(self.contentView.contentSize.width - _contentView.frame.size.width, 0) animated:YES];
        }
        
    }else{
        //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:D_LocalizedCardString(@"card_meitu_max_image_prompt")
        //                                                    message:D_LocalizedCardString(@"card_meitu_max_image_prompttext")
        //                                                   delegate:self
        //                                          cancelButtonTitle:nil
        //                                          otherButtonTitles:D_LocalizedCardString(@"card_meitu_max_image_promptDetermine"), nil];
        //        [alert show];
    }
    
}

- (void)startPintuButtonAction:(id)sender
{
    if (_delegateSelectImage && [_delegateSelectImage respondsToSelector:@selector(startPintuAction:)]) {
        [_delegateSelectImage startPintuAction:self];
    }
}

#pragma mark
#pragma mark ImageEditViewDelegate
- (void)responseToDelete:(id)sender
{
    JSImageEditView *imageEditView  = (JSImageEditView *)sender;
    [imageEditView removeFromSuperview];
    [_imageEditViewArray removeObject:imageEditView];
    [_imageassets removeObjectAtIndex:imageEditView.index];
    [self resetContentView];
}




- (void)resetContentView
{
    
   
    for (int i = 0; i < [_imageassets count]; i++) {
        JSImageEditView *tempEditView = [_imageEditViewArray objectAtIndex:i];
        tempEditView.index = i;
        tempEditView.frame = CGRectMake(i*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width,
                                        8,
                                        D_ImageEditView_Width,
                                        D_ImageEditView_Height);
//
//        [UIView animateWithDuration:0.2
//                              delay:0.1
//                            options:UIViewAnimationOptionCurveEaseInOut
//                         animations:^{
//                             tempEditView.frame = CGRectMake(i*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width,
//                                                             0,
//                                                             D_ImageEditView_Width,
//                                                             D_ImageEditView_Width);
//                         } completion:^(BOOL finished) {
//                             
//                         }];
        
    }
    [UIView animateWithDuration:0.3 animations:^{
        _contentView.contentSize = CGSizeMake([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width, _contentView.frame.size.height);
    }];
    self.countLabel.text = [NSString stringWithFormat:@"%lu/4",(unsigned long)[_imageassets count]];
}

- (void)reMoveAllResource
{
    [self.imageassets removeAllObjects];
    [self.imageEditViewArray removeAllObjects];
    for (UIView *v in  self.contentView.subviews) {
        [v removeFromSuperview];
    }
    [self resetContentView];
}

@end
